module Aggregator
  
  module EntryImageModel
    
    def self.included(base)
      base.class_eval do
        belongs_to :entry
      end
      base.extend(ClassMethods)
    end
    
    module ClassMethods

      def get_entry_images_for_aggregation(aggregation_id, options = {})
        sql = "SELECT entry_images.*, entries.permalink "
        sql << "FROM entry_images "
        sql << "INNER JOIN entries on entries.id = entry_images.entry_id "       
        sql << "INNER JOIN feeds on feeds.id = entries.feed_id "
        sql << "INNER JOIN aggregation_feeds ON aggregation_feeds.feed_id = feeds.id "
        sql << "WHERE aggregation_feeds.aggregation_id = ? "
        sql << "ORDER By entries.published_at DESC "
        add_limit!(sql, options)
        EntryImage.find_by_sql([sql, aggregation_id])
      end

    end
    
  end
end
